<?php
/*
  Microblog bot for game release notifications
  Copyright (C) 2017  Alexander Yakovlev

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace Source;

use \PHPHtmlParser\Dom;
use \Game;

abstract class Source {
  public $title;
  protected $dom;
  protected $period;
  protected $output;
  public $games;
  public function __construct() {
    global $lastrun;
    $this->dom = new Dom;
    $this->period = $lastrun;
    $this->output = '';
    $this->games = [];
  }
  /**
   * Function to start the section.
   * @param whether to return or print the text
   */
  protected function startSection($return = false) {
    if (FORMAT === 'MARKDOWN') {
      $text = "\n#### ".$this->title."\n";
    }
    if (FORMAT === 'HTML') {
      $text = "\n<spoiler title='".$this->title."'><h4>".$this->title."</h4>\n<ul>";
    }
    if ($return) {
      return $text;
    }
    $this->output .= $text;
  }
  protected function endSection() {
    if (FORMAT === 'HTML') {
      $text = "</ul></spoiler>\n";
    }
    $this->output .= $text;
  }
  abstract protected function parse();
  protected function get_text($url) {
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_RETURNTRANSFER => 1,
      CURLOPT_URL => $url,
      CURLOPT_CONNECTTIMEOUT => 30,
    ));
    $resp = curl_exec($curl);
    curl_close($curl);
    return $resp;
  }
  public function check() {
    try {
      $this->parse();
      return $this->output;
    } catch (Exception $e) {
      echo $e->getMessage();
      echo $e->getTraceAsString();
    }
  }
}
